package com.sl.clone.deepClone;

import lombok.Data;

import java.io.*;

/**
 * Created by lisulong on 2020/3/12.
 */
@Data
public class Person implements Serializable {
    private String name;
    private Integer age;
    private Address address;

    public Person deepClone() {
        Person p2 = null;
        Person p1 = this;
        PipedOutputStream out = new PipedOutputStream();
        PipedInputStream in = new PipedInputStream();
        try {
            in.connect(out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (ObjectOutputStream bo = new ObjectOutputStream(out);
             ObjectInputStream bi = new ObjectInputStream(in);) {
            bo.writeObject(p1);
            p2 = (Person) bi.readObject();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return p2;
    }
}
